{% extends 'customer_dashboard.html' %}

{% block content %}
<!-- axios 脚本引入 -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<style>
  .reset-card {
    max-width: 540px;
    margin: 40px auto;
    padding: 30px 40px;
    background: #f5f9ff;
    border-radius: 16px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  }

  .reset-card h2 {
    text-align: center;
    font-size: 24px;
    color: #1a73e8;
    margin-bottom: 24px;
  }

  .form-group {
    display: flex;
    flex-direction: column;
    margin-bottom: 18px;
    font-weight: 600;
    color: #333;
  }

  .form-group input,
  .form-group select {
    padding: 10px 14px;
    border: 1px solid #ccc;
    border-radius: 8px;
    font-size: 15px;
    margin-top: 6px;
  }

  .form-group input:focus,
  .form-group select:focus {
    outline: none;
    border-color: #3399ff;
    box-shadow: 0 0 6px rgba(51, 153, 255, 0.4);
  }

  .send-group {
    display: flex;
    gap: 10px;
  }

  .btn {
    background-color: #1a73e8;
    color: white;
    padding: 12px;
    font-size: 16px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    transition: background 0.3s;
  }

  .btn:hover {
    background-color: #1666c1;
  }

  .flash-message {
    list-style: none;
    padding: 0;
    margin-bottom: 20px;
    text-align: center;
  }

  .flash-message li {
    padding: 8px 12px;
    border-radius: 6px;
    margin-bottom: 8px;
  }

  .flash-success {
    color: green;
    background-color: rgba(0, 128, 0, 0.1);
  }

  .flash-error {
    color: red;
    background-color: rgba(255, 0, 0, 0.1);
  }
</style>

<div class="reset-card">
  <h2><i class="bi bi-shield-lock-fill"></i> 忘记密码重置</h2>

  {% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
      <ul class="flash-message">
        {% for category, message in messages %}
          <li class="flash-{{ 'error' if category == 'error' else 'success' }}">{{ message }}</li>
        {% endfor %}
      </ul>
    {% endif %}
  {% endwith %}

  <form method="post">
    <div class="form-group">
      <label for="cardID">选择银行卡号:</label>
      <select name="cardID" required>
        <option value="" disabled selected>请选择银行卡号</option>
        {% for card in user_cards %}
          <option value="{{ card.cardID }}" {% if request.form.cardID == card.cardID %}selected{% endif %}>
            {{ card.cardID }} （余额: {{ card.balance }} 元）
          </option>
        {% endfor %}
      </select>
    </div>

    <div class="form-group">
      <label for="name">姓名:</label>
      <input type="text" name="name" value="{{ request.form.name or '' }}" required>
    </div>

    <div class="form-group">
      <label for="pid">身份证号:</label>
      <input type="text" name="pid" value="{{ request.form.pid or '' }}" required>
    </div>

    <div class="form-group">
      <label for="phone">手机号:</label>
      <div class="send-group">
        <input type="text" name="phone" id="phone" value="{{ request.form.phone or '' }}" required style="flex: 1;">
        <button type="button" class="btn" onclick="sendCode()">发送验证码</button>
      </div>
    </div>

    <div class="form-group">
      <label for="code">验证码:</label>
      <input type="text" name="code" value="{{ request.form.code or '' }}" required>
    </div>

    <div class="form-group">
      <label for="new_password">新密码:</label>
      <input type="password" name="new_password" required>
    </div>

    <div class="form-group">
      <label for="confirm_password">确认密码:</label>
      <input type="password" name="confirm_password" required>
    </div>

    <button type="submit" class="btn" style="width: 100%; margin-top: 10px;">确认密码</button>
  </form>
</div>

<script>
  function sendCode() {
    const cardID = document.querySelector('select[name="cardID"]').value;
    const name = document.querySelector('input[name="name"]').value.trim();
    const pid = document.querySelector('input[name="pid"]').value.trim();
    const phone = document.querySelector('input[name="phone"]').value.trim();

    if (!cardID || !name || !pid || !phone) {
      alert('请填写完整的验证信息');
      return;
    }

    const button = event.target;
    button.disabled = true;
    button.textContent = '发送中...';

    axios.post('/customer/send_verification_code', { cardID, name, pid, phone }, {
      headers: { 'Content-Type': 'application/json' }
    })
    .then(res => {
      if (res.data.success) {
        alert(`验证码：${res.data.code}，请在${res.data.expire_seconds / 60}分钟内使用`);
        let countdown = 60;
        const interval = setInterval(() => {
          button.textContent = `${countdown--}秒后可重发`;
          if (countdown < 0) {
            clearInterval(interval);
            button.disabled = false;
            button.textContent = '发送验证码';
          }
        }, 1000);
      } else {
        alert('发送失败：' + res.data.message);
        button.disabled = false;
        button.textContent = '发送验证码';
      }
    })
    .catch(err => {
      alert('发送验证码失败，请稍后再试');
      button.disabled = false;
      button.textContent = '发送验证码';
      console.error(err);
    });
  }
</script>
{% endblock %}
